from plotly import graph_objs as go
# from plotly import version
# print(version)
def create_stack_bar_data(col, df):
aggregated = df[col].value_counts().sort_index()
x_values = aggregated.index.tolist()
y_values = aggregated.values.tolist()
return x_values, y_values
x1, y1 = create_stack_bar_data('an', accidents)
for i in range(len(x1)):
x1[i] += 2000
#x1 = x1[:-1]
#y1 = y1[:-1]
color1 = ['red']*4
color2 = ['green']*5
color1.extend(color2)
trace1 = go.Bar(x=x1, y=y1, opacity=0.75, name="year count", marker = dict(color = color1))
layout = dict(height=400, title='Year wise Number of Accidents in France', legend=dict(orientation="h"),
xaxis = dict(title = 'Year'), yaxis = dict(title = 'Number of Accidents'))
fig = go.Figure(data=[trace1], layout=layout);
iplot(fig);
accidents.an += 2000
dates = pd.to_datetime(accidents.an*10000+accidents.mois*100+accidents.jour,format='%Y%m%d')
accidents.an -= 2000
traces = []
for key, grp in dates.groupby(dates.dt.year):
#print(grp)
aggregated = grp.dt.month.value_counts().sort_index()
x_values = aggregated.index.tolist()
y_values = aggregated.values.tolist()
x1,y1 = x_values, y_values
x1 = [calendar.month_name[int(x)] for x in (x1)]
#x1, y1 = create_stack_bar_data('jour', accidents)
trace1 = go.Scatter(x=x1, y=y1, opacity=0.75, line = dict(
width = 1.5), name = str(key), marker = dict(color = np.random.randn(500)*key), mode = 'lines',
text = str(key))
layout = dict(height=400, title='Time Series of Accidents for each Year', legend=dict(orientation="h"));
traces.append(trace1)
fig = go.Figure(data= traces, layout=layout)
iplot(fig, filename='stacked-bar')
x1, y1 = create_stack_bar_data('catr', accidents)
x1 = ['Highway', 'National Road', 'Departmental Road', 'Communal Way', 'Off-Public Network', 'Parking Lot', 'Other']
trace1 = go.Bar(x=x1, y=y1, opacity=0.75, name="Category", marker=dict(color='blue'))
x2, y2 = create_stack_bar_data('circ', accidents)
x2 = ['Unknown','One Way', 'Bidirectional', 'Separated Carriageways', 'Variable Assignment Channels']
trace2 = go.Bar(x = x2, y = y2, opacity = 0.75, marker=dict(color='green'), name = "Traffic Flow")
x3, y3 = create_stack_bar_data('prof', accidents)
x3 = ['Unknown', 'Dish','Slope', 'Hill-Top', 'Hill-Bottom']
trace3 = go.Bar(x = x3, y = y3, opacity = 0.75, marker=dict(color='red'), name = "Road Gradient")
fig = tools.make_subplots(rows = 3, cols = 1)
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 2, 1)
fig.append_trace(trace3, 3, 1)
layout = dict(height=900, title='Accidents by Type of Road');
fig.layout.update(layout)
#fig['layout'].update(height=800,title='Accidents by Type of Road')
iplot(fig, filename='stacked-bar')
ageusers = accidents[['an_nais', 'catu']]
ageusers['age'] = 2016 - ageusers.an_nais
keydict = {1:'Driver', 2:'Passenger', 3:'Pedestrian', 4:'Pedestrian in Motion'}
traces = []
for key, grp in ageusers.groupby(ageusers.catu):
if(key < 4):
#aggregated = grp.an_nais.value_counts().sort_index()
x1 = grp.age.values
#y_values = aggregated.values.tolist()
#x1,y1 = x_values, y_values
#x1 = ['Driver','Passenger', 'Pedestrian', 'Pedestrian in Motion']
#x1, y1 = create_stack_bar_data('jour', accidents)
trace1 = go.Histogram(x=x1, opacity=0.5, name = keydict[key], marker = dict(color = '#34d5e4'))
layout = dict(height=400, title='Distribution of People involved in Accidents by Age',
legend=dict(orientation="h"), barmode = 'overlay');
traces.append(trace1)
fig = go.Figure(data= traces, layout=layout)
iplot(fig)